package com.example.zingalbumdownloader;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.example.zingalbumdownloader.adapter.MyAlbumAdapter;
import com.example.zingalbumdownloader.model.Album;
import com.example.zingalbumdownloader.utility.Html2xml;
import com.example.zingalbumdownloader.utility.MyResource;
import com.example.zingalbumdownloader.utility.StringUtils;
import com.example.zingalbumdownloader.utility.ZingMp3Parser_Playlist;

public class Album_Activity extends Activity {

	private int listview_bottom = 1;
	private int listview_check = 0;

	private EditText txtInputText;
	private ListView listView;

	private ArrayList<Album> arrayAlbum;
	private MyAlbumAdapter albumAdapter;
	// private ProgressDialog mprogressDialog;
	private ProgressBar mprogressBar;

	private String url;// "http://mp3.zing.vn/tim-kiem/playlist.html?q=em";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album);

		txtInputText = (EditText) findViewById(R.id.id_editTextInput);
		listView = (ListView) findViewById(R.id.id_listAlbum);
		// mprogressDialog = new ProgressDialog(Album_Activity.this);
		// mprogressDialog.setCancelable(true);
		// mprogressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mprogressBar = (ProgressBar) findViewById(R.id.id_progressBarSpin);

		arrayAlbum = new ArrayList<Album>();

		albumAdapter = new MyAlbumAdapter(Album_Activity.this,
				R.layout.item_myalbum, arrayAlbum);
		listView.setAdapter(albumAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(Album_Activity.this,
						AlbumDetail_Activity.class);

				Bundle bundle = new Bundle();
				bundle.putString("LINK", arrayAlbum.get(position).getHref());

				intent.putExtra("Package", bundle);
				startActivity(intent);
			}
		});

		listView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

				if (listview_bottom > listview_check
						&& listView.getLastVisiblePosition() == arrayAlbum
								.size() - 1) {
					listview_check++;
					doshowMoreAlbum();
					// Log.i("ZING", "bottom " + listview_bottom);
					// Log.i("ZING", "check " + listview_check);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

			}
		});
	}

	private void doshowMoreAlbum() {
		new showMoreAlbum()
				.execute((url + MyResource.page + (listview_bottom + 1)));
	}

	class showAlbum extends AsyncTask<String, Void, Void> {

		@Override
		protected void onPreExecute() {
			mprogressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				doShowAlbum(params[0]);
			} catch (XmlPullParserException e) {
				Log.e("ZING", "xml " + e.toString());
//				mprogressBar.setVisibility(View.GONE);
				this.cancel(true);
			} catch (IOException e) {
				Log.e("ZING", "xml io " + e.toString());
//				mprogressBar.setVisibility(View.GONE);
				this.cancel(true);
			}
			return null;
		}

		void doShowAlbum(String url) throws XmlPullParserException, IOException {
			ZingMp3Parser_Playlist parser_Playlist = new ZingMp3Parser_Playlist();
			String subs = StringUtils.getPlaylistString((StringUtils
					.getStringFromURLString(url)));
			Log.d("ZING", "get substring");
			String xml = Html2xml.getHtml2Xml(subs);
			Log.d("ZING", "html 2 xml");
			parser_Playlist.parse(xml, arrayAlbum);
		}

		@Override
		protected void onPostExecute(Void result) {
			albumAdapter.notifyDataSetChanged();
			mprogressBar.setVisibility(View.GONE);
			Log.d("ZING", "So luong Album: " + arrayAlbum.size());
		}
	}

	class showMoreAlbum extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... params) {
			try {
				doShowAlbum(params[0]);
			} catch (XmlPullParserException e) {
				Log.e("ZING", "xml " + e.toString());
				this.cancel(true);
			} catch (IOException e) {
				Log.e("ZING", "xml io " + e.toString());
				this.cancel(true);
			}
			return null;
		}

		void doShowAlbum(String url) throws XmlPullParserException, IOException {
			ZingMp3Parser_Playlist parser_Playlist = new ZingMp3Parser_Playlist();
			String subs = StringUtils.getPlaylistString((StringUtils
					.getStringFromURLString(url)));
			Log.d("ZING", "get substring");
			String xml = Html2xml.getHtml2Xml(subs);
			Log.d("ZING", "html 2 xml");
			parser_Playlist.parse(xml, arrayAlbum);
		}

		@Override
		protected void onPostExecute(Void result) {
			albumAdapter.notifyDataSetChanged();
			listview_bottom++;
			Log.d("ZING", "So luong Album: " + arrayAlbum.size());
		}
	}

	public void clickDelete(View v) {
		txtInputText.setText("");
	}

	public void clickSearch(View v) {
		if (!txtInputText.getText().toString().trim().equals("")) {
			url = StringUtils.generateSearchLink(txtInputText.getText()
					.toString());
			Log.d("ZING", url);
			arrayAlbum.removeAll(arrayAlbum);
			listview_bottom = 1;
			listview_check = 0;
			new showAlbum().execute(url);
		}
	}

}
